﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace WareDealer.Helper
{
    /// <summary>
    /// 通用类库
    /// </summary>
    public class Golemer
    {
        /// <summary>
        /// 获取时间戳
        /// </summary>
        /// <returns></returns>
        private string GetTimeStamp()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds).ToString();
        }
        /// <summary>  
        /// 获取当前时间戳  
        /// </summary>  
        /// <param name="bflag">为真时获取10位时间戳,为假时获取13位时间戳.</param>  
        /// <returns></returns>  
        public static string GetTimeStamp(bool bflag = true)
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            string ret = string.Empty;
            if (bflag)
                ret = Convert.ToInt64(ts.TotalSeconds).ToString();
            else
                ret = Convert.ToInt64(ts.TotalMilliseconds).ToString();

            return ret;
        }

        /// <summary>
        /// 清空文本中HTML标签
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string ClearHtml(string text)//过滤html,js,css代码
        {
            text = text.Trim();
            if (string.IsNullOrEmpty(text))
                return "";
            text = Regex.Replace(text, "<head[^>]*>(?:.|[\r\n])*?</head>", "");
            text = Regex.Replace(text, "<script[^>]*>(?:.|[\r\n])*?</script>", "");
            text = Regex.Replace(text, "<style[^>]*>(?:.|[\r\n])*?</style>", "");

            text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", ""); //<br> 
            text = Regex.Replace(text, "\\&[a-zA-Z]{1,10};", "");
            text = Regex.Replace(text, "<[^>]*>", "");

            text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", ""); //&nbsp;
            text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); //其它任何标记
            text = Regex.Replace(text, "[\\s]{2,}", " "); //两个或多个空格替换为一个

            text = text.Replace("'", "''");
            text = text.Replace("\r\n", "");
            text = text.Replace("  ", "");
            text = text.Replace("\t", "");
            return text.Trim();
        }

        private string Unicode2Chinese(string strUnicode)
        {
            string[] splitString = new string[1];
            splitString[0] = "\\u";
            string[] unicodeArray = strUnicode.Split(splitString, StringSplitOptions.RemoveEmptyEntries);
            StringBuilder sb = new StringBuilder();

            foreach (string item in unicodeArray)
            {
                byte[] codes = new byte[2];
                int code1, code2;
                code1 = Convert.ToInt32(item.Substring(0, 2), 16);
                code2 = Convert.ToInt32(item.Substring(2), 16);
                codes[0] = (byte)code2;//必须是小端在前
                codes[1] = (byte)code1;
                sb.Append(Encoding.Unicode.GetString(codes));
            }
            //Encoding.Unicode.GetString(Encoding.Unicode.GetBytes("\u4e00-\u9fa5")) 
            //var result = new Regex(@"\\u([0-9A-F]{4})", RegexOptions.IgnoreCase | RegexOptions.Compiled).Replace(s, m => "" + Convert.ToChar(Convert.ToUInt16(m.Result("$1"), 16)));

            return sb.ToString();
        }

        private string Chinese2Unicode(string strChinese)
        {
            string strUnicodes = string.Empty;
            foreach (char item in strChinese.ToCharArray())
            {
                strUnicodes += "\\u" + ((int)item).ToString("x"); //16进制
            }
            return strUnicodes;
        }
    }
}
